var g = 0,
  l = 0
var api = require('../../config/api.js');
var comm = require("../../utils/comm.js")
var app = getApp()
Page({
  /**
   * 页面的初始数据
   */
  data: {
    title: '',
    marketMoney:'',
    money: '',
    useNumber: '',
    startDate: '',
    endDate: '',
    createDate: '',
    createByName: '',
    updateDate: '',
    upateByName: '',
    statusName: '',
    address:'',
    status:'0',


    total: '0',

    toast1Hidden: true,
    modalHidden: true,
    modalHidden2: true,
    notice_str: '',

    files: [],
    files1: [],

    myAgentList: {
      hasMore: true,
      list: [],
      listSize: 0
    },

    curisOnSale: '',

    showMessage: false,
    messageContent: '',
    showDistpicker: false,

    categoryall: [], //所有分类
    categoryscond: [],//二级分类
    Categoryl1: [],
    Categorycodel1: [],
    CategoryIndexl1: '',
    Categoryl2: [],
    Categorycodel2: [],
    CategoryIndexl2: '',
    findnum:1,

    l1: g,
    l2: l,

    id: '',

  },


  //商品上下架
  ononsale: function () {
    if (this.data.status == '0') {
      this.setData({
        status: '1'
      })
    } else {
      this.setData({
        status: '0'
      })
    }
  },



  //修改设备套餐信息
  formSubmit: function (e) {
    var that = this;
    wx.showModal({
      title: '提示',
      content: '是否确认提交',
      confirmColor: "#43CBC9",
      success: (res) => {
        wx.showLoading({
          title: '加载中',
        })

        if (res.confirm) {
          wx.request({
            url: api.GoodsDeviceInfoUpd,
            data: {
              id: that.data.id,
              status: that.data.status
            },
            method: 'POST',
            header: {
              'Accept': 'application/json',
              'ACCESS-TOKEN': wx.getStorageSync('token')
            },
            success: function (res) {
              wx.hideLoading();
              if (res.data.success) {
                wx.showModal({
                  title: '提示',
                  content: "设备套餐信息修改成功！",
                  confirmColor: "#43CBC9",
                  showCancel: false,
                  success: function (res) {
                    if (res.confirm) {
                      console.log('用户点击确定')
                      wx.navigateBack({})
                    }
                  }
                });
                wx.hideLoading();
              } else {
                console.log("数据更新不成功！")
              }
            },
            fail: function () {
              wx.hideLoading();
              wx.showToast({
                title: res.data.msg,
                icon: 'none',
                duration: 2000
              });
            },
          })

        } else if (res.cancel) {
          console.log('用户点击取消')
          wx.hideLoading();
        }
      }
    })
  },

  //取消商品的信息修改
  canback: function () {
    wx.navigateBack({})
  },


  //根据套餐id获取套餐信息
  getGoodInfo: function () {
    var that = this;
    var ID = that.data.id
    comm.request(api.GoodsDeviceInfo + ID, {}, "GET").then(function (res) {
      console.log("1111")
      wx.showLoading({
        title: '加载中',
      })
      if (res.success) {
        that.setData({
          title: res.obj.title,
          marketMoney: res.obj.marketMoney,
          money: res.obj.money,
          useNumber: res.obj.useNumber,
          startDate: res.obj.startDate,
          endDate: res.obj.endDate,
          createDate: res.obj.createDate,
          updateDate: res.obj.updateDate,
          status: res.obj.status,
          address: res.obj.address
        });
        wx.hideLoading();
      }
    }).catch(function (res) {
      console.log("请求失败！")
    })
  },

  //根据商品分类编号查找商品类别
  findGoodCateory: function (sn) {
    console.log('sn= '+sn)
    var that = this
    var categoryall = that.data.categoryall
    var len = categoryall.length
    for (var i = 0; i < len; i++) {
      var list = categoryall[i].subGoodsCategoryList
      for (var j = 0; j < list.length; j++) {
        var id = list[j].id
        if (sn == id) {
          that.getCgetCateListfrs(i)
          that.setData({
            CategoryIndexl1: i,
            CategoryIndexl2: j,
          })
          return true
        }
      }
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    that.setCatego();
    that.setData({
      id: options.Id,
    });
  },


  //弹出确认框  
  modalTap: function (e) {
    this.setData({
      modalHidden: true
    })
  },
  confirm_one: function (e) {
    console.log(e);
    this.setData({
      modalHidden: true,
      toast1Hidden: false,
      notice_str: '提交成功'
    });
  },
  cancel_one: function (e) {
    console.log(e);
    this.setData({
      modalHidden: true,
      toast1Hidden: false,
      notice_str: '取消成功'
    });
  },
  //弹出提示框  
  modalTap2: function (e) {
    this.setData({
      modalHidden2: false
    })
  },
  modalChange2: function (e) {
    this.setData({
      modalHidden2: true
    })
  },
  toast1Change: function (e) {
    this.setData({
      toast1Hidden: true
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this
  },


  bindPickerChange: function (e) {
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      index: e.detail.value
    })
  },

  //获取一级分类信息
  getCatel1: function () {
    var that = this
    wx.request({
      url: api.GoodsCateGetOne,
      data: {},
      method: 'GET',
      header: {
        'Accept': 'application/json',
        'ACCESS-TOKEN': wx.getStorageSync('token')
      },
      success: function (res) {
        console.log(res.data)
        wx.hideLoading();
        if (res.data.code == '1') {
          var relsize = res.data.obj.categoryList.length;
          var categoryList = res.data.obj.categoryList;
          var Catl = that.data.Categoryl1;
          var Catco = that.data.Categorycodel1
          for (var i = 0; i < relsize; i++) {
            Catl = Catl.concat(categoryList[i].categoryName)
            Catco = Catco.concat(categoryList[i].id)
          }
          that.setData({
            Categoryl1: Catl,
            Categorycodel1: Catco
          })
        } else {
          // util.msg("错误", res.data.msg)
          console.log("网络通，数据不成功")
        }
      },
      fail: function (res) {
        wx.hideLoading();
        console.log(res.data)
        // util.msg("错误", "通讯失败")
      }
    })
  },

  //获取二级分类
  getCatel2: function (Id) {
    var that = this
    if (Id) {
      wx.request({
        url: api.GoodsCateGetById + Id,
        data: {},
        method: 'GET',
        header: {
          'Accept': 'application/json',
          'ACCESS-TOKEN': wx.getStorageSync('token')
        },
        success: function (res) {
          console.log(res.data)
          wx.hideLoading();
          if (res.data.code == '1') {
            var relsize = res.data.obj.currentSubCategory.length;
            var categoryList = res.data.obj.currentSubCategory;
            var Catl = [];
            var Catco = [];
            for (var i = 0; i < relsize; i++) {
              Catl = Catl.concat(categoryList[i].categoryName)
              Catco = Catco.concat(categoryList[i].id)
            }
            that.setData({
              Categoryl2: Catl,
              Categorycodel2: Catco
            })
          } else {
            // util.msg("错误", res.data.msg)
            console.log("网络通，数据不成功")
          }
        },
        fail: function (res) {
          wx.hideLoading();
          console.log(res.data)
          // util.msg("错误", "通讯失败")
        }
      })
    }
  },



  //获取商品分类的对象集合
  getAllGoodsCate: function (index) {
    var that = this
    comm.request(api.GoodsAllCate, {}, "GET").then(function (res) {
      if (res.success) {
        var catelist = res.obj
        that.setData({
          categoryall: catelist,
        })
        that.getCgetCateListfrs(index)
        if (that.data.findnum == 1){
          if (that.data.categoryall.length !== 0) {
            that.getGoodInfo()
            that.data.findnum ++
          }
        }
        
      }
    }).catch(function (res) {
      console.log("获取商品分类失败！")
    })
  },

  // 从所有集合中取出一分类
  getCgetCateListfrs: function (index) {
    var that = this
    var catelist = that.data.categoryall
    var relsize = that.data.categoryall.length
    var categoryscond = []
    var Catl = []
    var Catco = []
    for (var i = 0; i < relsize; i++) {
      Catl = Catl.concat(catelist[i].categoryName)
      Catco = Catco.concat(catelist[i].id)
    }
    categoryscond = categoryscond.concat(catelist[index].subGoodsCategoryList)
    that.setData({
      Categoryl1: Catl,
      Categorycodel1: Catco,
      categoryscond: categoryscond
    })

    that.getCateListIndex()
  },

  //根据一级参数index选择二级分类的集合
  getCateListIndex: function () {
    var that = this
    var categoryall = that.data.categoryscond
    var relsize = that.data.categoryscond.length
    var Catl = [];
    var Catco = [];
    for (var i = 0; i < relsize; i++) {
      Catl = Catl.concat(categoryall[i].categoryName)
      Catco = Catco.concat(categoryall[i].id)
    }
    that.setData({
      Categoryl2: Catl,
      Categorycodel2: Catco
    })
  },


  setCatego: function (g, l) {
    var that = this
    var g = g || 0
    var l = l || 0
    // //设置一级分类
    // that.getCatel1()
    // // 设置二级分类
    // that.getCatel2(this.data.Categorycodel1[g])
    that.getAllGoodsCate(g)
  },

  changeCate: function (e) {
    g = e.detail.value[0]
    l = e.detail.value[1]
    this.setCatego(g, l)
  },

  distpickerSure: function () {
    this.setData({
      CategoryIndexl1: g,
      CategoryIndexl2: l,
    })
    this.distpickerCancel()
  },

  showDistpicker: function () {
    this.setData({
      showDistpicker: true
    })
  },
  distpickerCancel: function () {
    this.setData({
      showDistpicker: false
    })
  },

  showMessage: function (text) {
    var that = this
    that.setData({
      showMessage: true,
      messageContent: text
    })
    setTimeout(function () {
      that.setData({
        showMessage: false,
        messageContent: ''
      })
    }, 3000)
  },


  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})